package com.gohome.gohomechat.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.gohome.gohomechat.R;
import com.gohome.gohomechat.model.entify.Friend;
import com.gohome.gohomechat.model.entify.Notice;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by dllo on 16/3/2.
 */
public class AddFriendListViewAdapter extends BaseAdapter {
    private List<Friend> datas;
    private onClickButtonListener listener;
    private Context context;

    public AddFriendListViewAdapter(Context context) {
        this.context = context;
    }

    public void addData(List<Friend> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }
    public void setOnClickButtonListener(onClickButtonListener listener){
        this.listener=listener;
    }

    @Override
    public int getCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return datas != null && datas.size() > 0 ? datas.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.addfriend_listview_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();
        Friend friend = datas.get(position);
        holder.addfriend_listview_item_username.setText(friend.getUserName());
        Picasso.with(context).load(friend.getHead()).into(holder.addfriend_listview_item_head);
        holder.addfriend_listview_item_btn_agree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.addFriend();
            }
        });
        return convertView;
    }

    class ViewHolder {
        private TextView addfriend_listview_item_username;
        private ImageView addfriend_listview_item_head;
        private Button addfriend_listview_item_btn_agree;

        public ViewHolder(View view) {
            addfriend_listview_item_username = (TextView) view.findViewById(R.id.addfriend_listview_item_username);
            addfriend_listview_item_head = (ImageView) view.findViewById(R.id.addfriend_listview_item_head);
            addfriend_listview_item_btn_agree = (Button) view.findViewById(R.id.addfriend_listview_item_btn_agree);
        }
    }

    public interface onClickButtonListener {
        void addFriend();
    }
}
